package com.uwlaser.rest;

import com.alibaba.fastjson.JSONObject;
import com.uwlaser.annotation.Log;
import com.uwlaser.domain.CraftStepDetail;
import com.uwlaser.service.CraftStepDetailService;
import com.uwlaser.service.dto.CraftStepDetailDto;
import com.uwlaser.service.dto.CraftStepDetailQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "工步详情管理")
@RequestMapping("/api/craftStepDetail")
public class CraftStepDetailController {

    private final CraftStepDetailService craftStepDetailService;

    @GetMapping
    @Log("查询工步详情")
    @ApiOperation("查询工步详情")
    @PreAuthorize("@el.check('craftStepDetail:list')")
    public ResponseEntity query(CraftStepDetailQueryParam query, Pageable pageable){
        return new ResponseEntity<>(craftStepDetailService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增工步详情")
    @ApiOperation("新增工步详情")
    @PreAuthorize("@el.check('craftStepDetail:add')")
    public ResponseEntity create(MultipartFile file , String data){
        CraftStepDetailDto craftStepDetailDto = JSONObject.parseObject(data, CraftStepDetailDto.class);

        return new ResponseEntity<>(craftStepDetailService.insert(file ,craftStepDetailDto),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改工步详情")
    @ApiOperation("修改工步详情")
    @PreAuthorize("@el.check('craftStepDetail:edit')")
    public ResponseEntity update(MultipartFile file , String data){
        CraftStepDetailDto resources = JSONObject.parseObject(data, CraftStepDetailDto.class);
        craftStepDetailService.updateById(file,resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除工步详情")
    @ApiOperation("删除工步详情")
    @PreAuthorize("@el.check('craftStepDetail:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        craftStepDetailService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出工步详情")
    @ApiOperation("导出工步详情")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('craftStepDetail:list')")
    public void download(HttpServletResponse response, CraftStepDetailQueryParam query) throws IOException {
        craftStepDetailService.download(craftStepDetailService.queryAll(query), response);
    }*/

}
